package com.cangsan.erhai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.TabCourse;
import com.cangsan.erhai.entity.TabFootball;
import com.cangsan.erhai.service.TabCourseService;
import com.cangsan.erhai.service.TabFootballService;
import com.cangsan.erhai.util.StringUtil;
import com.cangsan.erhai.vo.TabCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class TabCourseController {
    @Autowired
    private TabCourseService tabCourseService;
    @Autowired
    private TabFootballService footballService;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<TabCourseVo> queryWrapper = new QueryWrapper<>();
        Page<TabCourseVo> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        if(StringUtil.isNotEmpty(pageBean.getQuery())){
            queryWrapper.eq("contest_id",pageBean.getQuery());
        }

        queryWrapper.eq("is_del",0);
        queryWrapper.orderByDesc("create_time");
        IPage<TabCourseVo> tabCourseVoIPage = tabCourseService.selectTabCourseVoPage(page, queryWrapper);
        Map<String,Object> result = new HashMap<>();
        List<TabCourseVo> records = tabCourseVoIPage.getRecords();
        result.put("dataList",records);
        result.put("total",tabCourseVoIPage.getTotal());
        return R.ok(result);
    }

    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody TabCourse tabCourse){
        TabFootball t1 = footballService.getById(tabCourse.getCourseMasterTeam());
        tabCourse.setMasterFootballIcon(t1.getFootballIcon());
        tabCourse.setMasterFootballName(t1.getFootballName());
        TabFootball t2 = footballService.getById(tabCourse.getCourseAwayTeam());
        tabCourse.setAwayFootballIcon(t2.getFootballIcon());
        tabCourse.setAwayFootballName(t2.getFootballName());
        tabCourseService.saveOrUpdate(tabCourse);
        return R.ok();
    }

    @GetMapping("/getFootballList/{contestId}")
    public R getFootballList(@PathVariable Long contestId ){
        List<TabFootball> footballList = tabCourseService.getFootballList(contestId);
        return R.ok(footballList);
    }

    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        tabCourseService.removeById(id);
        return R.ok("删除成功");
    }
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id) {
        List<TabCourse> dataList = new ArrayList<>();
        TabCourse byId = tabCourseService.getById(id);
        dataList.add( tabCourseService.getById(id));
        return R.ok(dataList);
    }



}
